package org.springframework.boot.netty.argumentResolver;


import org.springframework.boot.netty.exception.ConvertException;
import org.springframework.boot.netty.listener.Message;

import java.util.Map;

public class HandlerMethodArgumentResolverComposite {


   private final Map<String, HandlerMethodArgumentResolver> handlerMethodArgumentResolverMap ;


   public HandlerMethodArgumentResolverComposite(Map<String, HandlerMethodArgumentResolver> handlerMethodArgumentResolverMap){
        this.handlerMethodArgumentResolverMap = handlerMethodArgumentResolverMap;
    }

    //该解析器是否支持parameter参数的解析
    public String supportsParameterComposite(Class<?> receiveClass,MethodParameter methodParameter){

        for (Map.Entry<String, HandlerMethodArgumentResolver> argumentResolverEntry : this.handlerMethodArgumentResolverMap.entrySet()) {
            if(argumentResolverEntry.getValue().supportsParameter(receiveClass,methodParameter)){
                return argumentResolverEntry.getKey();
            }
        }
        return null;
    }

    //将方法参数从给定请求解析为参数值并返回
    public Object resolveArgument(Message message, MethodParameter methodParameter,String arguementKey) throws ConvertException {

        HandlerMethodArgumentResolver argumentResolver = this.handlerMethodArgumentResolverMap.get(arguementKey);
        return argumentResolver.resolveAndConvertArgument(message,methodParameter);
    }

}